package loggerhelper

import (
	
	
	

	logrus 
)

// redis类型
type FormatType int32

const (
	FormatType_JSON FormatType = 0
	FormatType_Text FormatType = 1
)

//Option 设置key行为的选项
//@attribute MaxTTL time.Duration 为0则不设置过期
//@attribute AutoRefresh string 需要为crontab格式的字符串,否则不会自动定时刷新
type Options struct {
	Type             FormatType
	DisableTimeField bool
	TimeFormat       string
	Level            logrus.Level
	DefaultFieldMap  logrus.FieldMap
	ExtFields        map[string]interface{}
	Output           io.Writer
	Hooks            []logrus.Hook
}

var DefaultOpts = Options{
	Type:       FormatType_JSON,
	TimeFormat: time.RFC3339Nano,
	Level:      logrus.DebugLevel,
	DefaultFieldMap: logrus.FieldMap{
		logrus.FieldKeyTime:        "time",
		logrus.FieldKeyLevel:       "level",
		logrus.FieldKeyMsg:         "event",
		logrus.FieldKeyLogrusError: "logrus_error",
		logrus.FieldKeyFunc:        "caller",
		logrus.FieldKeyFile:        "file",
	},
	ExtFields: map[string]interface{}{},
	Hooks:     []logrus.Hook{},
}

// Option configures how we set up the connection.
type Option interface {
	Apply(*Options)
}

// func (emptyOption) apply(*Options) {}
type funcOption struct {
	f func(*Options)
}

func ( *funcOption) ( *Options) {
	.f()
}

func ( func(*Options)) *funcOption {
	return &funcOption{
		f: ,
	}
}

//WithTextFormat Init函数的参数,用于设置使用text格式替换json格式
func () Option {
	return newFuncOption(func( *Options) {
		.Type = FormatType_Text
	})
}

//WithDisableTimeField Init函数的参数,用于设置使用text格式替换json格式
func () Option {
	return newFuncOption(func( *Options) {
		.DisableTimeField = true
	})
}

//WithTimeFormat Init函数的参数,用于设置使用指定的时间解析格式,默认为RFC3339Nano
func ( string) Option {
	return newFuncOption(func( *Options) {
		.TimeFormat = 
	})
}

//parseLevel 将字符串转为`logrus.Level`,未知的字符串默认匹配为"logrus.DebugLevel"
func ( string) logrus.Level {
	,  := logrus.ParseLevel()
	if  != nil {
		fmt.Printf("未知的等级`%s`,使用默认值`Debug`\n", )
		return logrus.DebugLevel
	}
	return 
}

//WithLevel Init函数的参数,用于设置log等级
func ( string) Option {
	return newFuncOption(func( *Options) {
		.Level = parseLevel()
	})
}

//AddHooks Init函数的参数,用于增加钩子
func ( ...logrus.Hook) Option {
	return newFuncOption(func( *Options) {
		if .Hooks == nil {
			.Hooks = []logrus.Hook{}
		}
		.Hooks = append(.Hooks, ...)
	})
}

//WithDefaultFieldMap Init函数的参数,用于设置默认字段的新命名
func ( logrus.FieldMap) Option {
	return newFuncOption(func( *Options) {
		.DefaultFieldMap = 
	})
}

//AddExtField Init函数的参数,用于增加扩展字段
func ( string,  interface{}) Option {
	return newFuncOption(func( *Options) {
		if .ExtFields == nil {
			.ExtFields = map[string]interface{}{: }
		} else {
			.ExtFields[] = 
		}
	})
}

//WithExtFields Init函数的参数,用于设置扩展字段
func ( map[string]interface{}) Option {
	return newFuncOption(func( *Options) {
		if .ExtFields == nil || len(.ExtFields) == 0 {
			.ExtFields = 
		} else {
			for ,  := range  {
				.ExtFields[] = 
			}
		}
	})
}

//WithOutput Init函数的参数,用于设置log的写入io
func ( io.Writer) Option {
	return newFuncOption(func( *Options) {
		.Output = 
	})
}